const server = require('dgram');

// 创建一个socket
// 创建方式1
// const serverSocket = new dgram.socket()

// 创建方式2
const serverSocket = server.createSocket('udp4');  // 对应的ipv4，udp6 =》 ipv6

serverSocket.on('error', (err) => {
    console.log(`服务器异常：\n${err.stack}`);
    serverSocket.close();
});

serverSocket.on('message', (msg, rinfo) => {
    console.log(`服务器收到：${msg} 来自 ${rinfo.address}:${rinfo.port}`);
});

serverSocket.on('listening', () => {
    const address = serverSocket.address();
    console.log(`服务器监听 ${address.address}:${address.port}`);
});

serverSocket.bind(41234, '127.0.0.1');

// console.log('服务器开启，等待数据');